"use strict";
{
    class Node {
        constructor(element = null, next = null) {
            this.element = element;
            this.next = next;
        }
    }
    class LinkList {
        constructor() {
            this.head = new Node(1);
            this.length = 1;
            this.head.next = this.head;
        }
        find(item) {
            let currNode = this.head;
            while (currNode.next && currNode.next.element !== 1) {
                if (currNode.element === item) {
                    break;
                }
                else {
                    currNode = currNode.next;
                }
            }
            return currNode;
        }
        findPrevious(item) {
            // 查找一个元素的前一个元素
            let currNode = this.head;
            while (currNode.next) {
                if (currNode.next.element === item) {
                    break;
                }
                else {
                    currNode = currNode.next;
                }
            }
            return currNode;
        }
        insert(newElement, item) {
            let node = new Node(newElement);
            let currNode = this.find(item);
            node.next = currNode.next;
            currNode.next = node;
            this.length++;
        }
        remove(item) {
            let prevNode = this.findPrevious(item);
            if (prevNode.next) {
                prevNode.next = prevNode.next.next;
            }
            this.length--;
        }
        display() {
            let currNode = this.head;
            while (currNode.next) {
                console.log(currNode.element);
                if (currNode.next && currNode.next.element !== 1) {
                    currNode = currNode.next;
                }
                else {
                    break;
                }
            }
        }
    }
    let list = new LinkList();
    list.insert(2, 1);
    for (let i = 3; i <= 41; i++) {
        list.insert(i, i - 1);
    }
    const killOne = (list) => {
        var _a, _b;
        let start = list.head;
        while (list.length >= 3) {
            if ((_a = list.head.next) === null || _a === void 0 ? void 0 : _a.next) {
                let current = (_b = start.next) === null || _b === void 0 ? void 0 : _b.next;
                start = current === null || current === void 0 ? void 0 : current.next;
                list.head = start;
                // console.log(current.element);
                list.remove(current.element);
            }
        }
        // console.log(list.head.element);
        // console.log(list.head.next.element);
    };
    killOne(list);
}
